package com.tamir.template.express.parse;

import com.tamir.template.bean.TLogicNodeType;
import com.tamir.template.bean.TNode;
import com.tamir.template.config.PatternConfig;
import com.tamir.template.config.TemplateNodeTypeConfig;
import com.tamir.template.util.LogicOperateUtil;
import com.tamir.template.util.StringUtil;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

/**
 * @author : tanggh
 * @description :text结点处理类,包括属性("${user.name}")获取结点处理
 * @date : 2016/10/27
 */
public class DefaultTextHandleParse extends AbstractLogicHandleParse
{
    private Logger logger = Logger.getLogger(DefaultTextHandleParse.class);

    @Override
    public List<TNode> parse(TNode parentNode, String oddText)
    {
        List<TNode> list = new ArrayList<TNode>();
        TLogicNodeType oppsiteLogicNodeType = null;
        if(parentNode.gettLogicType() != null ){
            oppsiteLogicNodeType = TemplateNodeTypeConfig
                    .getInstance()
                    .getOppositeLogicType
                            (parentNode.gettLogicType
                                    ());
        }
        Matcher oppositeMatcher = PatternConfig.elsePattern.matcher(oddText);
        String leftText = StringUtil.subLeftString(oddText, oppositeMatcher);
        if(oppsiteLogicNodeType != null){
            //重置匹配
            oppositeMatcher.reset();
            String rightText = StringUtil.subRightString(oddText, oppositeMatcher);
            //如果相反结点右边文本不为空,解析相反结点
            if(StringUtil.isNotEmpty(rightText)){
                TNode oppositeNode = new TNode();
                //目前只有if有else结点,else结点跟if结一样不需要inputKey
                oppositeNode.settLogicType(oppsiteLogicNodeType);
                //相反结点的父结点跟原结点的父结点相同
                oppositeNode.setParentNode(parentNode.parentNode);
                parentNode.setOppositeLogicNode(oppositeNode);
                oppositeNode.setSubNodeList(parse(oppositeNode, rightText));
            }
        }
        //如果相反结点左侧文本不为空,则认为左侧本文为真正本结点需要解析文本
        if(StringUtil.isNotEmpty(leftText)){
            oddText = leftText;
        }
        if(StringUtil.isNotEmpty(oddText)){
            Matcher matcher = attrPt.matcher(oddText);
            int index = 0;
            int start = 0;
            int end = 0;
            while (matcher.find()){
                start = matcher.start();
                end = matcher.end();
                if(start > index){
                    list.add(this.genTNodeText(parentNode, oddText.substring(index, start)));
                }
                list.add(this.genTNodeAttribute(parentNode, oddText.substring(start, end)));
                logger.debug(start + ":" + end);
                logger.debug(oddText.substring(start, end));
                index = end;
            }
            if(oddText.length() - index > 0){
                TNode node = new TNode();
                node.settLogicType(TemplateNodeTypeConfig.getInstance().getRegisterLogicTypeByKey
                        (TemplateNodeTypeConfig.NODE_TYPE_TEXT));
                list.add(this.genTNodeText(parentNode, oddText.substring(index)));
            }
        }



        return list;
    }

    /**
     * 生成文本结点
     * @param parentNode
     * @param oddText
     * @return
     */
    private TNode genTNodeText(TNode parentNode, String oddText){
        TNode node = new TNode();
        node.settLogicType(TemplateNodeTypeConfig.getInstance().getRegisterLogicTypeByKey
                (TemplateNodeTypeConfig.NODE_TYPE_TEXT));
        node.setText(oddText);
        node.setParentNode(parentNode);
        return node;
    }

    /**
     * 设置node的输入输出关键字和对应的值
     *
     * @param node
     */
    public void parseIOKeyForNode(TNode node)
    {
        if(node != null && node.gettLogicType().gettLogicType().equalsIgnoreCase
                (TemplateNodeTypeConfig.NODE_TYPE_ATTRIBUTE)){
            node.setInputKey(LogicOperateUtil.getInputKey(node.getRealExpress()));
        }
    }

    public static void main(String[] args)
    {
        String text = "测试广西${user.u.a}我在中间${user.name}${b.a}${c=d}圧";
        DefaultTextHandleParse parse = new DefaultTextHandleParse();
//        System.out.println(parse.attrPt.matcher("${a=b}").matches());
        List nodeList = parse.parse(null, text);
        System.out.println(nodeList);


    }
}
